package org.millioncall.yueyoga.admin.service.impl;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.yueyoga.admin.dao.OrderDAO;
import org.millioncall.yueyoga.admin.dao.UserDetailDAO;
import org.millioncall.yueyoga.admin.model.Order;
import org.millioncall.yueyoga.admin.model.TinyOrderModel;
import org.millioncall.yueyoga.admin.service.OrderService;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {

	private OrderDAO orderDAO;
	private UserDetailDAO userDetailDAO;

	public OrderDAO getOrderDAO() {
		return orderDAO;
	}

	public void setOrderDAO(OrderDAO orderDAO) {
		this.orderDAO = orderDAO;
	}

	@Override
	public Order findById(Integer id) {
		return this.orderDAO.findById(id);
	}

	@Override
	public PaginatedList<Order> findInPage(int pageNo, int pageSize) {
		return this.orderDAO.findInPage(pageNo, pageSize);
	}

	@Override
	public int save(Order order) {
		return this.orderDAO.save(order);
	}

	@Override
	public int deleteByIds(String[] ids) {
		return this.orderDAO.deleteByIds(ids);
	}

	@Override
	public PaginatedList<Order> findAll() {
		return this.orderDAO.findAll();
	}

	@Override
	public Order findByOrder(Order order) {
		return this.orderDAO.findByOrder(order);
	}

	@Override
	public PaginatedList<Order> findByMemberId(Integer pageNo, Integer limit,
			Integer memberId) {
		return this.orderDAO.findByMemberId(pageNo, limit, memberId);
	}

	@Override
	public PaginatedList<Order> findOrdersByCdts(int pageNo, int pageSize,
			Map<String, Object> map) {
		PaginatedList<Order> orders = this.orderDAO.findOrdersByCdts(pageNo,
				pageSize, map);
		if (!orders.isEmpty()) {
			for (int i = 0; i < orders.size(); i++) {
				Order order = orders.get(i);
				if (order != null
						&& StringUtils.isNotBlank(order.getSellerId())) {
					order.setUd(userDetailDAO.findUserDetailById(order
							.getSellerId()));
				}
			}
		}
		return orders;
	}

	@Override
	public PaginatedList<Order> findGoodsOrdersByCdts(int start, int limit,
			Map<String, Object> map) {
		PaginatedList<Order> orders = this.orderDAO.findGoodsOrdersByCdts(
				start, limit, map);
		if (!orders.isEmpty()) {
			for (int i = 0; i < orders.size(); i++) {
				Order order = orders.get(i);
				if (order != null
						&& StringUtils.isNotBlank(order.getSellerId())) {
					order.setUd(userDetailDAO.findUserDetailById(order
							.getSellerId()));
				}
			}
		}
		return orders;
	}

	public UserDetailDAO getUserDetailDAO() {
		return userDetailDAO;
	}

	public void setUserDetailDAO(UserDetailDAO userDetailDAO) {
		this.userDetailDAO = userDetailDAO;
	}

	@Override
	public PaginatedList<TinyOrderModel> findAllBuyRecord(int pageNo,
			int pageSize, Map<String, Object> params) {
		return this.orderDAO.findAllBuyRecord(pageNo, pageSize, params);
	}

	@Override
	public int disableOrder(String[] Ids) {
		int del = 0;
		if (Ids == null || Ids.length == 0) {
			throw new RuntimeException(" 未提交要作废订单的编号");
		} else {
			try {
				del = this.orderDAO.disableOrder(Ids);
				return del;
			} catch (Exception e) {
				return 0;
			}
		}
	}

	@Override
	public int getOrderEnabledCountByMemberId(Map<String, Object> map) {
		return this.orderDAO.getOrderEnabledCountByMemberId(map);
	}

	@Override
	public int updateOrderType(Order order) {
		return this.orderDAO.updateOrderType(order);
	}

	@Override
	public int disableOrder(Map<String, Object> map) {
		return this.orderDAO.disableOrder(map);
	}
}
